package com.example.hms.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class TokenUtil {
    String tokens = "sdaawss";
    public String getToken(String userId,String roleName,long time){
        String token = JWT.create()
                .withClaim("userId",userId)
                .withClaim("roleName",roleName)
                .withClaim("time",time)
                .sign(Algorithm.HMAC256(tokens));
        return token;
    }
    public Map<String,Object> checkToken(String token){
        Map<String,Object> map = new HashMap<>();
        DecodedJWT jwt = JWT.require(Algorithm.HMAC256(tokens)).build().verify(token);
        Claim userId = jwt.getClaim("userId");
        Claim roleName = jwt.getClaim("roleName");
        Claim time = jwt.getClaim("time");
        map.put("userId",userId.asString());
        map.put("roleName",roleName.asString());
        map.put("time",time.asLong());
        return map;
    }
}
